package com.jxmobi.car.core.endpoints;

import com.jxmobi.car.core.model.CarModel;
import com.jxmobi.car.core.service.CarService;
import com.jxmobi.car.core.vo.CarNewVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author fei <a href="mailto:xchen@jxmobi.com">Email the author</a>
 * @version 1.0 7/28/16
 */
@RestController
@RequestMapping(value = "/cars")
public class CarController {
    private static final Logger log = LoggerFactory.getLogger(CarController.class);

    @Autowired private CarService carService;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseEntity<?> findCarsByPage(Pageable pageable) {
        Page<CarModel> carModelPages = this.carService.findCarsByPage(pageable);
        return new ResponseEntity<>(carModelPages, HttpStatus.OK);
    }


    @RequestMapping(value = "/{model}", method = RequestMethod.GET)
    public ResponseEntity<CarModel> findByModel(@PathVariable("model") String model) {
        CarModel carModel = this.carService.findByModel(model);

        return new ResponseEntity<CarModel>(carModel, HttpStatus.OK);
    }


    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity<CarModel> saveCar(@Valid @RequestBody CarNewVo newCar) {
        CarModel carModel = this.carService.saveNewCar(newCar);

        return new ResponseEntity<CarModel>(carModel, HttpStatus.OK);
    }

}
